qui(function(require, exports, module) {
	Vodad.Util = $.extend(true, {}, EpgWeb.Util, {
			formatter: {
			    type: function (v, r) {
                    return Vodad.Config.VODAD_TYPE[v];
                },
                time: function (v, r) {
                    return v.substring(0, 19);
                },
                img: function (v, r) {
                        return '<img src="' + r.picAddress + '" width="42" height="42">';
                },
                operate:function(v,r) {
                    var str = "<a href='javascript:void(0);' onclick='Vodad.Util.del(" + r.id + ", \"" + r.uuid + "\")' target='_blank'>删除</a>";
                    return str;
                },
                contentType:function(v, r) {
                    return Vodad.Config.VODAD_CONTENT_TYPE[v];
                },
                fileSize: function(v, r) {
                    return v + "|" + (v / 1024).toFixed(2) + "|" + (v / 1024 /1024).toFixed(4);
                }
            },

            del: function(id, uuid){
                var self = this;
                if(window.confirm('是否从素材列表中删除该数据？')) {
                    if (typeof(uuid) != "undefined" && uuid != "") {
                        self.delUploadVideo(uuid);
                    }
                    var params = {id : id};
                    Vodad.Dao.delete(params, function(){
                        $('.query-btn').click();
                    });
                    return true;
                }
            },
            calculateDate: function (str) {//month 7days yestoday today
                var from_date = "", to_date = "";
                var d = new Date();
                var defaultEnd = this.formatDateTimeFormat(d);
                this.setZeroTime(d);
                if (str == "today") {
                    from_date = this.formatDateTimeFormat(d);
                    to_date = defaultEnd;
                }
                if (str == "yesterday") {
                    d.setDate(d.getDate() - 1);
                    from_date = this.formatDateTimeFormat(d);
                    this.setDayLastTime(d);
                    to_date = this.formatDateTimeFormat(d);
                }

                if (str == "7days") {
                    d.setDate(d.getDate() - 6);
                    from_date = this.formatDateTimeFormat(d);
                    to_date = defaultEnd
                }
                if (str == "month") {
                    d.setMonth(d.getMonth() - 1);
                    from_date = this.formatDateTimeFormat(d);
                    to_date = defaultEnd
                }
                if (str == "absToday") {
                    from_date = this.formatDateTimeFormat(d);
                    this.setDayLastTime(d);
                    to_date = this.formatDateTimeFormat(d);
                }

                if (str == "absToday+") {
                    this.setZeroTime(d);
                    d.setHours(10);
                    from_date = this.formatDateTimeFormat(d);
                    d.setDate(d.getDate() + 1);
                    to_date = this.formatDateTimeFormat(d);
                }

                this.bindDate("GET_opTime", "LET_opTime", { maxDate: to_date, maxDateTime: new Date(to_date.replace(/-/g, "/")), minDate: from_date, minDateTime: new Date(from_date.replace(/-/g, "/"))})
                $("input[name=GET_opTime]").datetimepicker("setDate", new Date(from_date.replace(/-/g, "/")));
                $("input[name=LET_opTime]").datetimepicker("setDate", new Date(to_date.replace(/-/g, "/")));
            },
            isValidVideoImage: function (filetype) {
                return $.inArray(filetype.toLowerCase(), ["jpg", "png"]) >= 0;
            },
            isValidImage: function (filetype) {
                return $.inArray(filetype.toLowerCase(), ["bmp", "jpg", "jpeg", "png", "tiff", "gif", "pcx", "tga", "exif", "fpx", "psd", "cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "wmf"]) >= 0;
            },
            isValidVideo: function (filetype) {
                return $.inArray(filetype.toLowerCase(), ["wmv", "asf", "asx", "rm", "rmvb", "mpg", "mpeg", "mpe", "3gp", "mov", "mp4", "m4v", "avi", "dat", "mkv", "flv", "vob", "ts"]) >= 0;
            },
            verifyForm: function (data, type) {
                var self = this;
                var checkArr = [];

                switch (type) {
                    case 0:
                        checkArr.push({value: data.address, error: "请上传一个视频素材", type: "not_empty"});
                        checkArr.push({value: data.picAddress, error: "请上传一张封面图", type: "not_empty"});
                        checkArr.push({value: data.name, error: "请填写新文件名 ", type: "not_empty"});
                        checkArr.push({value: data.duration, error: "请填写视频时长", type: "not_empty"});
                        checkArr.push({value: data.duration, error: "视频时长格式错误", type: "number"});
                        checkArr.push({value: data.duration, error: "中插视频时长不能大于120秒", type: "le", other: 120});
                        break;
                    case 1:
                        checkArr.push({value: data.picAddress, error: "请上传一个图片素材", type: "not_empty"});
                        checkArr.push({value: data.name, error: "请填写新文件名 ", type: "not_empty"});
                        break;
                }
                checkArr.push({value: Com.string.getStrLength(data.name), error: "新文件名不能超过40个字", type: "le", other: 80});
                checkArr.push({value: Com.string.getStrLength(data.remark), error: "备注不能超过100个字", type: "le", other: 200});
                checkArr.push({value: data.fileType, error: "文件类型不能为空", type: "not_empty"});
                checkArr.push({value: data.fileSize, error: "文件大小不能为空", type: "not_empty"});
                if (checkArr.length > 0) {
                    return EpgWeb.Plugin.Validate.batchCheck(checkArr);
                }
                return true;
            },
            startUploadFile: function (params, callback) {
                var self = this;
                var url = Vodad.Config.UPLOAD_URL;
                url = url + "?t=1";

                $.each(params, function (k, v) {
                    url = url + "&" + k + "=" + v;
                });
                $("#dlg_upload_waiting").dialog("moveToTop").dialog("open");
                $.ajax({
                    url: url,
                    method: "post",
                    cache: false,
                    contentType: false,
                    processData: false,
                    xhr: function () {
                        var xhr = jQuery.ajaxSettings.xhr();
                        return xhr;
                    },
                    data: params.file,
                    success: function (data) {
                        if (data.code == "A00000") {
                            console.log("success upload");
                            callback.call(this, data.data, params);
                        }
                        $("#dlg_upload_waiting").dialog("close");
                    },
                    error: function (data) {
                        $("#dlg_upload_waiting").dialog("close");
                    }
                })
            },
            startUploadVideo: function (params, callback) {
                var self = this;
                //当前时间戳
                var timestamp = Date.parse(new Date()) / 1000;
                var data = Vodad.Config.UPLOAD_VIDEO_PARAMS;
                var url = data.url + params.file.name;
                var sign = md5(data.access_key + url + "PUT" + data.secret_key + parseInt(timestamp/data.ttl));
                //console.log("access_key=" + data.access_key + " | url=" + data.url + " | http_method=PUT | secret_key=" + data.secret_key + " | quotient=" + parseInt(timestamp/data.ttl));
                //console.log("sign=" + sign);
                url = url + "?access=" + data.access_key + "&ttl=" + data.ttl + "&sign=" + sign;
                $("#dlg_upload_progress").dialog("moveToTop").dialog("open");

                $.ajax({
                    url: url,
                    method: "put",
                    cache: false,
                    contentType: false,
                    processData: false,
                    xhr: function () {
                        var xhr = jQuery.ajaxSettings.xhr();
                        xhr.upload.addEventListener("progress" , self.progressFunction, false);
                        return xhr;
                    },
                    data: params.file,
                    success: function (data) {
                        if (data.uuid != null) {
                            console.log("success upload");
                            callback.call(this, data, params);
                        }
                        $('#dlg_upload_progress .progress-bar').css('width', '0%');
                        $("#dlg_upload_progress").dialog("close");
                    },
                    error: function (data) {
                        $('#dlg_upload_progress .progress-bar').css('width', '0%');
                        $("#dlg_upload_progress").dialog("close");
                    }
                })
            },
					
            //上传进度实现方法，上传过程中会频繁调用该方法
            progressFunction: function (evt) {
                // event.total是需要传输的总字节，event.loaded是已经传输的字节。如果event.lengthComputable不为真，则event.total等于0
                if (evt.lengthComputable) {//
                    var progress = Math.round(evt.loaded / evt.total * 100);
                    $('#dlg_upload_progress .progress-bar').css('width', progress + '%');
                    $('#percentage').text(evt.loaded + " / " + evt.total);
                    $('#progressPercent').text(progress + '%');
                }
            },
            delUploadVideo: function (uuid) {
                var self = this;
                //当前时间戳
                var timestamp = Date.parse(new Date()) / 1000;
                var data = Vodad.Config.UPLOAD_VIDEO_PARAMS;
                var url = data.url + uuid;
                var sign = md5(data.access_key + url + "DELETE" + data.secret_key + parseInt(timestamp/data.ttl));
                url = url + "?access=" + data.access_key + "&ttl=" + data.ttl + "&sign=" + sign;
                $.ajax({
                    url: url,
                    method: "delete",
                    cache: false,
                    contentType: false,
                    success: function (data) {
                        console.log(data);
                    },
                    error: function (data) {
                    }
                })
            },
		});
});